﻿using System;
using System.Security.AccessControl;
using Company.IO.Interfaces;
using Company.System.IO.IntegrationTests.Properties;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Company.System.IO.IntegrationTests
{
    public partial class DirectoryTest  : TestBase<IDirectory>
    {
        private const string _SCreateDirectoryMethodname = "CreateDirectory";
        private const string _SCreateDirectory = "CreateDirectory";

        [TestMethod]
        public void CreateDirectory_PathValid_DirectoryExists()
        {
            //Arrange
            string path = global::System.IO.Path.Combine(global::System.IO.Directory.GetCurrentDirectory(),
                                                         _SCreateDirectory);
            path = global::System.IO.Path.GetFullPath(path);

            if (global::System.IO.Directory.Exists(path))
            {
                global::System.IO.Directory.Delete(path);
            }

            //Act
            Directory.CreateDirectory(path);

            //Assert
            Assert.IsTrue(global::System.IO.Directory.Exists(path));
        }

        [TestMethod]
        public void CreateDirectory_PathValidSecurity_DirectoryExists()
        {
            //Arrange
            string path = global::System.IO.Path.Combine(global::System.IO.Directory.GetCurrentDirectory(),
                                                         _SCreateDirectory);
            path = global::System.IO.Path.GetFullPath(path);
            var security = new DirectorySecurity(global::System.IO.Directory.GetCurrentDirectory(),
                                                 AccessControlSections.Access);

            if (global::System.IO.Directory.Exists(path))
            {
                global::System.IO.Directory.Delete(path);
            }

            //Act
            Directory.CreateDirectory(path, security);

            //Assert
            Assert.IsTrue(global::System.IO.Directory.Exists(path));
        }

        [TestMethod]
        public void CreateDirectory_PathNull_ArgumentNullException()
        {
            PathNull_ArgumentNullException(_SCreateDirectoryMethodname);
        }

        [TestMethod]
        public void CreateDirectory_PathEmpty_ArgumentException()
        {
            PathEmpty_ArgumentException(_SCreateDirectoryMethodname);
        }

        [TestMethod]
        public void CreateDirectory_PathContaisOnlyWhitespace_ArgumentException()
        {
            PathContaisOnlyWhitespace_ArgumentException(_SCreateDirectoryMethodname);
        }

        [TestMethod]
        public void CreateDirectory_PathContaisOnlyColonChar_ArgumentException()
        {
            PathContaisOnlyColonChar_ArgumentException(_SCreateDirectoryMethodname);
        }

        [TestMethod]
        public void CreateDirectory_PathContaisInvalidPathChar_ArgumentException()
        {
            PathContaisInvalidPathChar_ArgumentException(_SCreateDirectoryMethodname);
        }

        [TestMethod]
        public void CreateDirectory_PathTooLong_PathTooLongException()
        {
            PathTooLong_PathTooLongException(_SCreateDirectoryMethodname, Resources.PathTooLong);
        }

        [TestMethod]
        public void CreateDirectory_PathNullSecurityDefault_ArgumentNullException()
        {
            //Arrange
            var security = new DirectorySecurity(global::System.IO.Directory.GetCurrentDirectory(),
                                                 AccessControlSections.Access);

            Type[] parameterTypes = GetTypeArray<string, DirectorySecurity>();
            object[] parameterValues = GetValueArray(SStringNull, security);
            //Act
            ExpectException<ArgumentNullException>(_SCreateDirectoryMethodname, parameterTypes, parameterValues);
            //Assert
        }

        [TestMethod]
        public void CreateDirectory_PathEmptySecurityDefault_ArgumentException()
        {
            //Arrange
            var security = new DirectorySecurity(global::System.IO.Directory.GetCurrentDirectory(),
                                                 AccessControlSections.Access);

            Type[] parameterTypes = GetTypeArray<string, DirectorySecurity>();
            object[] parameterValues = GetValueArray(string.Empty, security);

            //Act
            ExpectException<ArgumentException>(_SCreateDirectoryMethodname, parameterTypes, parameterValues);

            //Assert
        }

        [TestMethod]
        public void CreateDirectory_PathContainsOnlyWhitespaceSecurityDefault_ArgumentException()
        {
            //Arrange
            var security = new DirectorySecurity(global::System.IO.Directory.GetCurrentDirectory(),
                                                 AccessControlSections.Access);

            Type[] parameterTypes = GetTypeArray<string, DirectorySecurity>();
            object[] parameterValues = GetValueArray(Resources.PathContainsOnlyWhitspace, security);

            //Act
            ExpectException<ArgumentException>(_SCreateDirectoryMethodname, parameterTypes, parameterValues);

            //Assert
        }

        [TestMethod]
        public void CreateDirectory_PathContainsOnlyColonCharSecurityDefault_ArgumentException()
        {
            //Arrange
            var security = new DirectorySecurity(global::System.IO.Directory.GetCurrentDirectory(),
                                                 AccessControlSections.Access);

            Type[] parameterTypes = GetTypeArray<string, DirectorySecurity>();
            object[] parameterValues = GetValueArray(Resources.PathContainsOnlyColonChar, security);

            //Act
            ExpectException<ArgumentException>(_SCreateDirectoryMethodname, parameterTypes, parameterValues);

            //Assert
        }

        [TestMethod]
        public void CreateDirectory_PathContainsInvalidPathCharSecurityDefault_ArgumentException()
        {
            //Arrange
            var security = new DirectorySecurity(global::System.IO.Directory.GetCurrentDirectory(),
                                                 AccessControlSections.Access);

            char[] invalidChars = global::System.IO.Path.GetInvalidPathChars();
            Type[] parameterTypes = GetTypeArray<string, DirectorySecurity>();
            object[] parameterValues = GetValueArray(new string(invalidChars), security);

            //Act
            ExpectException<ArgumentException>(_SCreateDirectoryMethodname, parameterTypes, parameterValues);

            //Assert
        }

        [TestMethod]
        public void CreateDirectory_PathTooLongSecurityDefault_PathTooLongException()
        {
            //Arrange
            var security = new DirectorySecurity(global::System.IO.Directory.GetCurrentDirectory(),
                                                 AccessControlSections.Access);

            Type[] parameterTypes = GetTypeArray<string, DirectorySecurity>();
            object[] parameterValues = GetValueArray(Resources.PathTooLong, security);

            //Act
            ExpectException<global::System.IO.PathTooLongException>(_SCreateDirectoryMethodname, parameterTypes,
                                                                    parameterValues);
            //Assert
        }
    }
}